/*
Content: Create relevant and isolated events
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/



	
*********************************
***** MONTHLY EVENTS ************
*********************************

use  "$data/eventsdata/allevents_monthly_soep_final.dta", clear 

* For each country in the data expand to have a panel month-year for 1970 to 2018
tab year
drop if startmy==.
keep soep_corigin_id
bys soep_corigin_id: gen unique = _n
keep if unique == 1 

expand 588 // (2018-1970+1)*12=588

	* Create variable startmy for all month-year combinations from 1970 to 2019
	gen counter = 1 
	bys soep_corigin_id: gen startmy = 120	// 120 is Jan1970 in the month-year number classification
	bys soep_corigin_id: gen spell = _n - 1 
	bys soep_corigin_id: replace startmy = startmy + spell 
	format startmy %tm		
	* Merge back the original event data keeping only the variables of interes
	merge 1:1 startmy soep_corigin_id using "$data/eventsdata/allevents_monthly_soep_final.dta", gen(_merge_terror) keepusing(soep_corigin_id startmy terror nkill_terror year month armedconflict) keep(1 3)

cap drop year
cap drop month
g year=year(dofm(startmy))
g month=month(dofm(startmy))
sort soep_c startmy year month


* If in a given month there is no event assign a zero
foreach var in  nkill_terror terror  armedconflict   {
 replace `var' = 0 if _merge_terror == 1 // months with no terror	
}
	

*******************************************
*** RELEVANT AND ISOLATED TERROR EVENTS ***
*******************************************

* Yearly totals
foreach var in  nkill_terror terror  armedconflict {
	bys year soep_corigin_id: egen `var'_y = total(`var') 
}
	
* RELEVANCE: Difference with respect to the yearly average in the 3, 4 and 5 years before
foreach var in  nkill_terror terror  armedconflict   {
			
			* Generate yearly averages (not total)
			bys year soep_corigin_id: egen `var'_ya = mean(`var')
			
			* Generate difference with respect to the yearly average of the previous three years
			bys soep_corigin_id (startmy): gen `var'_m3y = (`var' - ((`var'_ya[_n-12] + `var'_ya[_n-24] + `var'_ya[_n-36])/3))
			
			* Generate difference with respect to the yearly average of the previous four years 
			bys soep_corigin_id (startmy): gen `var'_m4y = (`var' - ((`var'_ya[_n-12] + `var'_ya[_n-24] + `var'_ya[_n-36]+ `var'_ya[_n-48])/4))
			
			* Generate difference with respect to the yearly average of the previous five years
			bys soep_corigin_id (startmy): gen `var'_m5y = (`var' - ((`var'_ya[_n-12] + `var'_ya[_n-24] + `var'_ya[_n-36]+ `var'_ya[_n-48]+ `var'_ya[_n-60])/5))
}

* There is at least 1 relevant terror event
foreach event in  terror  armedconflict {	
foreach var in	 m5y m4y m3y {
g event`event'_`var'=`event'_`var'>=1 
}
}		
	
* Absolute number of killed	
foreach var in 10 20 30 40 50 {
gen event`var'_k = 0
replace event`var'_k = 1 if nkill_terror >= `var' & nkill_terror!=.		
}
	


	drop _merge_terror
	drop unique counter spell
	cap drop month
	g month=month(dofm(startmy))
	drop if year<1987
	compress


* ISOLATE: To use multiple events in one year, we need to isolate those events around time bands
bysort soep_corigin_id year: g obs=_n
cap drop months_year
bysort soep_corigin_id (startmy): g months_year=_n

*This accumulates the events over the past 1 month, 2 months and 3 months
cap drop x 
foreach event in  terror   armedconflict   {	
foreach var in	 m5y m4y m3y {
		
	rangestat (sum) event`event'_`var'_p30=event`event'_`var', interval(months_year,-1,0) by(soep_corigin_id)
replace event`event'_`var'_p30=. if year==1987 & month==1
	
		rangestat (sum) event`event'_`var'_p60=event`event'_`var', interval(months_year,-2,0) by(soep_corigin_id)
replace event`event'_`var'_p60=. if year==1987 & inrange(month,1,2)

		rangestat (sum) event`event'_`var'_p90=event`event'_`var', interval(months_year,-3,0) by(soep_corigin_id)
replace event`event'_`var'_p90=. if year==1987 & inrange(month,1,3)
			
}
}


* Identify "isolated" events, e.g. months were in the previous 1, 2 or 3 months there were no events
foreach event in  terror   armedconflict   {	
foreach var in	 m5y m4y m3y {
g keep_e`event'_`var'_p30=event`event'_`var'_p30<=1
g keep_e`event'_`var'_p60=event`event'_`var'_p60<=1
g keep_e`event'_`var'_p90=event`event'_`var'_p90<=1		
}
}


* Identify the date of these isolated events
foreach event in  terror   armedconflict   {	
foreach var in	 m5y m4y m3y {
g date_e`event'_`var'_p30=startmy if event`event'_`var'==1 & keep_e`event'_`var'_p30==1
g date_e`event'_`var'_p60=startmy if event`event'_`var'==1 & keep_e`event'_`var'_p60==1
g date_e`event'_`var'_p90=startmy if event`event'_`var'==1 & keep_e`event'_`var'_p90==1			
}
}
format date_e*_p*0 %tm



* Allocates the date of isolated events to the pre-and post-reference bands
foreach event in  terror   armedconflict   {	
foreach var in	 m5y m4y m3y {
rangestat (min) relevant`event'_`var'_p30=date_e`event'_`var'_p30 if keep_e`event'_`var'_p30==1, interval(startmy, -1, 1) by(soep_corigin_id)
rangestat (min) relevant`event'_`var'_p60=date_e`event'_`var'_p60 if keep_e`event'_`var'_p60==1, interval(startmy, -2, 2) by(soep_corigin_id)
rangestat (min) relevant`event'_`var'_p90=date_e`event'_`var'_p90 if keep_e`event'_`var'_p90==1, interval(startmy, -3, 3) by(soep_corigin_id)
}
}
format relevant*_p*0 %tm

* Creates the distance in months from the isolated event
foreach event in  terror   armedconflict   {	
foreach var in	 m5y m4y m3y {
g distance`event'_`var'_p30=startmy- relevant`event'_`var'_p30 if relevant`event'_`var'_p30!=.
g distance`event'_`var'_p60=startmy- relevant`event'_`var'_p60 if relevant`event'_`var'_p60!=.
g distance`event'_`var'_p90=startmy- relevant`event'_`var'_p90 if relevant`event'_`var'_p90!=.
}
}


* Make sure that for every isolated event there is a control and treatment group within the corresponding bandwidth  (this is to be consistent with the IEB)
foreach event in  terror   armedconflict   {	
foreach var in	 m5y m4y m3y {
g select`event'_`var'_p30=1 if date_e`event'_`var'_p30!=. & distance`event'_`var'_p30[_n-1]==-1 & distance`event'_`var'_p30[_n+1]==1
g select`event'_`var'_p60=1 if date_e`event'_`var'_p60!=. & distance`event'_`var'_p60[_n-1]==-1 & distance`event'_`var'_p60[_n+1]==1 & distance`event'_`var'_p60[_n-2]==-2 & distance`event'_`var'_p60[_n+2]==2
g select`event'_`var'_p90=1 if date_e`event'_`var'_p90!=. & distance`event'_`var'_p90[_n-1]==-1 & distance`event'_`var'_p90[_n+1]==1 & distance`event'_`var'_p90[_n-2]==-2 & distance`event'_`var'_p90[_n+2]==2 & distance`event'_`var'_p90[_n-3]==-3 & distance`event'_`var'_p90[_n+3]==3
}
}

foreach event in  terror   armedconflict   {	
foreach var in	 m5y m4y m3y {
replace date_e`event'_`var'_p30=. if select`event'_`var'_p30!=1
replace date_e`event'_`var'_p60=. if select`event'_`var'_p60!=1
replace date_e`event'_`var'_p90=. if select`event'_`var'_p90!=1		
}
}

foreach event in terror   armedconflict   {	
foreach var in	 m5y m4y m3y {

cap drop relevant`event'_`var'_p30 relevant`event'_`var'_p60 relevant`event'_`var'_p90
rangestat (min) relevant`event'_`var'_p30=date_e`event'_`var'_p30 if keep_e`event'_`var'_p30==1, interval(startmy, -1, 1) by(soep_corigin_id)
rangestat (min) relevant`event'_`var'_p60=date_e`event'_`var'_p60 if keep_e`event'_`var'_p60==1, interval(startmy, -2, 2) by(soep_corigin_id)
rangestat (min) relevant`event'_`var'_p90=date_e`event'_`var'_p90 if keep_e`event'_`var'_p90==1, interval(startmy, -3, 3) by(soep_corigin_id)

}
}
format relevant*_p*0 %tm

* Create the distance in months from the isolated event
foreach event in  terror  armedconflict   {	
foreach var in	 m5y m4y m3y {
cap drop  distance`event'_`var'_p30 distance`event'_`var'_p60 distance`event'_`var'_p90
g distance`event'_`var'_p30=startmy- relevant`event'_`var'_p30 if relevant`event'_`var'_p30!=.
g distance`event'_`var'_p60=startmy- relevant`event'_`var'_p60 if relevant`event'_`var'_p60!=.
g distance`event'_`var'_p90=startmy- relevant`event'_`var'_p90 if relevant`event'_`var'_p90!=.
}
}

* Create an event identifier - combine date and country
foreach var in	 m5y m4y m3y {
sort soep_c startmy
cap drop identterror_`var'_p90
g identterror_`var'_p90=string(soep_c)+"_"+string(relevantterror_`var'_p90) if relevantterror_`var'_p90!=. & distanceterror_`var'_p90!=. // event identifier
}

foreach var in	 m5y m4y m3y {
sort soep_c startmy
cap drop identarmedconflict_`var'_p90
g identarmedconflict_`var'_p90=string(soep_c)+"_"+string(relevantarmedconflict_`var'_p90) if relevantarmedconflict_`var'_p90!=. & distancearmedconflict_`var'_p90!=. // event identifier
}

 
 * Intensity of events
foreach x in 10 30 50   {	
foreach var in	 m5y m4y m3y {
	cap drop terror`var'p90_event`x'k
	cap drop temp2
g temp2=event`x'_k if distanceterror_`var'_p90==0 // number of killed in the month of the attack
bysort identterror_`var'_p90: egen terror`var'p90_event`x'k=max(temp2) // by event assign to treated and control
sort soep_c startmy
drop temp2
}
}

foreach x in	10 30 50  {
foreach var in	 m5y m4y m3y  {

label var  terror`var'p90_event`x'k  "`x' or >killed"
label def terror`var'p90_event`x'k  0 "0 or <`x' killed" 1 "`x' or >killed", replace 
label val terror`var'p90_event`x'k  terror`var'p90_event`x'k 
}
}

compress
save "$globalterror/final\terror_monthlydata_soep_final.dta", replace 



	
	